﻿//===========================================================================================================================
// Cuberry - http://www.cuberry.net
// Copyright (c) 2012
// by Peacequare® - Amir Moussa
//===========================================================================================================================
// This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License 
// as published by the Free Software Foundation, either version 3 of the License, or any later version.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// This program is distributed in the hope that it will be useful.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//
// See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//===========================================================================================================================

using System.Collections.Generic;
using System.Globalization;
using P2.Cuberry.Framework.BOLBase;
using P2.Cuberry.Framework.Helper;
using System.Collections.ObjectModel;
using System.Data;
using System.ComponentModel;
using Microsoft.Practices.Prism.Events;
using System;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Unity;
using System.Windows;
using Microsoft.Practices.Prism.Commands;
using System.Diagnostics;

namespace P2.Cuberry.Modules.Framework.ApplicationConfigurator
{
    class ApplicationConfiguratorFindPresenter : IApplicationConfiguratorFindPresenter, INotifyPropertyChanged
    {

        //public ApplicationConfiguratorFindPresenter(IApplicationConfiguratorFindView view, Services.IApplicationConfiguratorService service)
        //{
        //    View = view;
        //    //this.Model = new BusinessEntities.Module();
        //    //this.Service = service;

        //    this.ModuleAddCommand = new DelegateCommand<object>(OnModuleAddExecute, OnModuleAddCanExecute);
        //    this.ModuleFindCommand = new DelegateCommand<object>(OnModuleFindExecute, OnModuleFindCanExecute);
        //    this.ModuleClrCommand = new DelegateCommand<object>(OnModuleClrExecute, OnModuleClrCanExecute);

        //    //ControlRights = Model.GetControls(Properties.Settings.Default.ModuleName, "ApplicationConfiguratorFindView");

        //    View.Presenter = this;
        //}

        #region "Members"




        public int StatusID
        {
            //get { return Model.StatusID; }
            //set
            //{
            //    if (Model.StatusID != value)
            //    {
            //        Model.StatusID = value;
            //        OnPropertyChanged("StatusID");
            //    }
            //}
            get { return -1; }
        }


        #endregion


        #region "Reference Tables"
        public List<P2.Cuberry.Framework.Helper.Workflows.WorkflowStatus> WorkflowStatusses { get; set; }
        #endregion


        #region Commands

        public DelegateCommand<object> ModuleAddCommand { get; set; }
        public DelegateCommand<object> ModuleClrCommand { get; set; }
        public DelegateCommand<object> ModuleFindCommand { get; set; }
        
        void OnModuleAddExecute(object sourceAccount)
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.DefaultExt = ".dll";
            dlg.Filter = "Modules (.dll)|*.dll";
            Nullable<bool> result = dlg.ShowDialog();
            if (result == true)
            {
                FileVersionInfo vi = FileVersionInfo.GetVersionInfo(dlg.FileName);
                string[] arrParts = vi.OriginalFilename.Split('.');
                string moduleName = arrParts[arrParts.Length-2];

                ModuleRepository.SaveModule(moduleName, vi.FileVersion, dlg.FileName);
            }
        }
        

        bool OnModuleAddCanExecute(object sourceAccount)
        {
            return true;
            //P2.Cuberry.Framework.Security.User.ControlRight controlRight;
            //if (ControlRights.TryGetValue("ModuleAddBtn", out controlRight))
            //    return controlRight.IsActive;
            //else
            //    return false;
        }


        void OnModuleFindExecute(object sourceAccount)
        {
            //this.View.UpdateFocusedControl();
            //if (ModuleFind != null)
            //    ModuleFind(null, new DataEventArgs<BusinessEntities.Module>(this.Model));
        }


        bool OnModuleFindCanExecute(object sourceAccount)
        {
            return ModuleRepository.IsAddModuleAllowed();
            
        }

        void OnModuleClrExecute(object sourceAccount)
        {
        }


        bool OnModuleClrCanExecute(object sourceAccount)
        {
            return true;
        }

        #endregion


        public IApplicationConfiguratorFindView View { get; set; }
        //public Services.IApplicationConfiguratorService Service;
        //public BusinessEntities.Module Model { get; set; }

        public Dictionary<string, P2.Cuberry.Framework.Security.User.ControlRight> ControlRights;

        public P2MessageStack MessageStack = new P2MessageStack();

        //public event EventHandler<DataEventArgs<BusinessEntities.Module>> ModuleFind = delegate { };
        //public event EventHandler<DataEventArgs<BusinessEntities.Module>> ModuleAdd = delegate { };

        #region Property Changed

        public event PropertyChangedEventHandler PropertyChanged;


        private void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler Handler = PropertyChanged;
            if (Handler != null) Handler(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion


    }
}
